package com.probiz.estore.customer.service.impl;

import java.util.List;

import com.probiz.estore.common.model.customer.Favorite;
import com.probiz.estore.common.model.customer.FavoriteProduct;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.customer.service.FavoriteManager;
import com.probiz.estore.customer.dao.FavoriteDao;


/**
 * Manager implementation for Favorite, responsible for business processing, and communicate between web and persistence layer.
 */
public class FavoriteManagerImpl extends GenericManagerImpl<Favorite> implements FavoriteManager {

	private FavoriteDao favoriteDao = null;


	public void setFavoriteDao(FavoriteDao favoriteDao) {
		this.favoriteDao = favoriteDao;
	}

	@Override
	protected void initManager() {
		dao = favoriteDao;
	}

	public boolean addFavorite(Integer appuserId, Integer itemId, Short favoriteType) {
		if (!this.testDuplicate(appuserId, itemId, favoriteType)) {
			return false;
		}
		
		favoriteDao.addFavorite(appuserId, itemId, favoriteType);
		return true;
	}

	public boolean addFavoriteProduct(Integer appuserId, Integer itemId) {
		return this.addFavorite(appuserId, itemId, Favorite.CONST_FAVORITE_PRODUCT);
	}
	
	private boolean testDuplicate(Integer appuserId, Integer itemId, Short favoriteType) {
		List<Favorite> list = favoriteDao.getFavorites(appuserId);
		for (Favorite f : list) {
			if (f.getItemId().equals(itemId) && f.getType().equals(favoriteType)) {
				return false;
			}
		}
		return true;
	}

	public boolean deleteFavorite(Integer favoriteId, Integer appuserId) {
		Favorite favorite = favoriteDao.getById(favoriteId);
		if (favorite != null && favorite.getAppuserId().equals(appuserId)) {
			favoriteDao.delete(favorite);
		}
		return true;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.FavoriteManager#deleteFavoriteProducts(java.lang.Integer)
	 */
	public void deleteFavoriteProducts(Integer productId) {
		favoriteDao.deleteFavoriteProducts(productId);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.FavoriteManager#getFavoriteProducts(java.lang.Integer)
	 */
	public List<FavoriteProduct> getFavoriteProductss(Integer appuserId) {
		return favoriteDao.getFavoriteProducts(appuserId);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.FavoriteManager#getFavorites(java.lang.Integer)
	 */
	public List<Favorite> getFavorites(Integer appuserId) {
		return favoriteDao.getFavorites(appuserId);
	}

	@Override
	protected void preDelete(Favorite entity) {
		
	}

	@Override
	protected void preSave(Favorite entity) {
		
	}
	
	/**
	 * 用户收藏夹
	 * @param appuserId 用户ID
	 * @param maxSize 最大个数
	 * @return
	 */
	public List<Favorite> getFavoritesByCustomer(Integer appuserId, Integer maxSize){
		return favoriteDao.getFavoritesByCustomer(appuserId, maxSize);
	}

	public long countCustomerFavorite(Integer appuserId) {
		return favoriteDao.countCustomerFavorite(appuserId);
	}

}
